package pdf;

import com.itextpdf.text.*;
import com.itextpdf.text.pdf.*;
import com.itextpdf.tool.xml.XMLWorkerFontProvider;
import com.itextpdf.tool.xml.XMLWorkerHelper;
import freemarker.template.Configuration;
import freemarker.template.Template;
import lombok.AllArgsConstructor;
import lombok.Data;

import java.io.*;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class PdfUtil {
    public static void main(String[] args) throws Exception {

        String path = "/work/public/bold/early/src/main/resources/pay/";
        String outputFile = path + "outPay.pdf";
        //模板 和 数据 整合
        String htmlStr = htmlAndFreemarker(path, "template.html", getDada(path));
        // html 页面 转 pdf
        generate(htmlStr, outputFile);

//        byte[] b = Files.readAllBytes(Paths.get(outputFile));
//        String encodeToString = Base64.getEncoder().encodeToString(b);
//        System.out.println(encodeToString);
    }


    private static Map<String, Object> getDada(String path) {
        Map<String, Object> variables = new HashMap<>();
        List<For> forList = new ArrayList<>();

        forList.add(new For("收款人1", "账号11", "11,111,11"));
        forList.add(new For("收款人2", "账号22", "22,22,22"));
        forList.add(new For("收款人3", "账号33", "33,33,33"));
        variables.put("var1", "备注备注备注备注备注备注备注备注备注备注");
        variables.put("forList", forList);
        variables.put("path", path);
        return variables;
    }

    public static String htmlAndFreemarker(String path, String template, Map<String, Object> variables) throws Exception {
        Configuration config = new Configuration();
        File file = new File(path);
        try {
            config.setDirectoryForTemplateLoading(file);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
        Template tp = config.getTemplate(template); //获取具体模板
        StringWriter stringWriter = new StringWriter();
        BufferedWriter writer = new BufferedWriter(stringWriter);
        tp.setEncoding("UTF-8");
        tp.process(variables, writer);
        String htmlStr = stringWriter.toString();
        writer.flush();
        writer.close();
        return htmlStr;
    }

    public static void generate(String htmlStr, String outPdfFile) throws Exception {
        Document document = new Document();

        PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(outPdfFile));
        document.open();
        InputStream htmlStream = new ByteArrayInputStream(htmlStr.getBytes());
        XMLWorkerHelper.getInstance().parseXHtml(writer, document, htmlStream, null, new XMLWorkerFontProvider() {
            @Override
            public Font getFont(final String fontname, final String encoding, final boolean embedded, final float size, final int style, final BaseColor color) {
                BaseFont bf = null;
                try {
                    bf = BaseFont.createFont("STSong-Light", "UniGB-UCS2-H", BaseFont.NOT_EMBEDDED);
                } catch (Exception e) {
                    e.printStackTrace();
                }

                Font font = new Font(bf, size, style, color);
                font.setColor(color);
                return font;
            }
        });

        Image image1 = Image.getInstance("/work/public/bold/early/src/main/resources/pay/log1@3x.png");
        image1.scaleAbsolute(145, 30);
        image1.setAbsolutePosition(80, 800);
        document.add(image1);

        Image image2 = Image.getInstance("/work/public/bold/early/src/main/resources/pay/log2@3x.png");
        image2.scaleAbsolute(110, 75);
        image2.setAbsolutePosition(440, 480-70*2);
        document.add(image2);

        document.close();
    }


    @Data
    @AllArgsConstructor
    public static class For {
        private String name;
        private String code;
        private String money;
    }
}
